package com.xl.rentkeeper.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.xl.rentkeeper.log.QLog;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by zjm on 2014/11/18.
 */
public class ImgUtil {

    private static int calculateInSampleSize(BitmapFactory.Options options,
                                             int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > width) {
            int heightRatio = Math.round((float) height
                    / (float) reqHeight);
            int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = Math.max(heightRatio,widthRatio);
        } else {
            int heightRatio = Math.round((float) height
                    / (float) reqWidth);
            int widthRatio = Math.round((float) width / (float) reqHeight);
            inSampleSize = Math.max(heightRatio,widthRatio);
        }
        inSampleSize = Math.max(inSampleSize,1);
        QLog.d("calcSize", "h:" + height + " w " + width + " sample " + inSampleSize);
        return inSampleSize;
    }

    public static class CompressInfo {
        public long fileSize;
        public String path;
        public String md5;
        public int width;
        public int height;
    }

    public static CompressInfo computeInfo(String filePath) {
        File f = new File(filePath);
        if (f.exists()) {
            try {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(filePath, options);
                CompressInfo info = new CompressInfo();
                info.md5 = f.getName();
                info.path = filePath;
                info.height = options.outHeight;
                info.width = options.outWidth;
                return info;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static CompressInfo compressBitmap(String filePath,int reqWidth,int reqHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        int oriW = options.outWidth;
        int oriH = options.outHeight;
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        int sW = options.outWidth / options.inSampleSize;
        int sH = options.outHeight / options.inSampleSize;
        options.inJustDecodeBounds = false;
        Bitmap bm = BitmapFactory.decodeFile(filePath, options);
        if(bm == null){
            return  null;
        }
        String path = AppPath.ImgPath+System.currentTimeMillis();
        File temp = new File(path);
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(temp);
            if (bm.compress(Bitmap.CompressFormat.JPEG, 70, out)) {
                out.flush();
                out.close();
                bm.recycle();
                String md5 = Util.fileMD5(path);
                if (md5 == null)
                    return null;
                String targetPath = AppPath.ImgPath+md5;
                File target = new File(targetPath);
                if (target.exists())
                    target.delete();
                temp.renameTo(new File(targetPath));
                CompressInfo info = new CompressInfo();
                info.fileSize = new File(targetPath).length();
                info.md5 = md5;
                info.path = targetPath;
                BitmapFactory.Options options2 = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(targetPath, options2);
                info.width = options2.outWidth;
                info.height = options2.outHeight;
                QLog.d("imgutil", "sw:" + sW + " sh:" + sH + " w:" + options2.outWidth + " h:" + options2.outHeight + " oW:" + oriW + " oH:" + oriH);
                return info;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(out != null)
                    out.close() ;
            } catch (IOException e) {
                e.printStackTrace();
            }
            File f = new File(path);
            if (f.exists())
                f.delete();
        }
        return null ;
    }

//    public static void loadHeadImage(ImageView image) {
//        String headPath = UserAction.getInstance().getLoginUser().getHeadPath();
//        if (!TextUtils.isEmpty(headPath))
//            ImageLoader.getInstance().displayImage(headPath,image, App.sDefaultOptions);
//    }
}
